/**
 * Created by mxk on 2017/6/9.
 * 显示功能的树
 */

var setting = {
    view: {
        selectedMulti: false,
        showLine: true,
        showText: true,
        showIcon: false
    },
    check: {
        enable: true
    },
    callback: {
        onClick: function (e, treeId, treeNode, clickFlag) {
            zTreeObject.checkNode(treeNode, !treeNode.checked, true);
        }
    },
    data: {
        simpleData: {
            enable: true
        }
    }
};

var zTreeObject;
function  loadTree(obj) {
    var _funcIdList=','+$(obj).val()+',';
    var html='' +
    '<div id="roleTree"  class="ztree" >'+
        '</div>';
    dialog({
        title: '菜单列表',
        content: html,
        cancel: true,
        okValue:'添加',
        ok:function () {
            var treeObj=$.fn.zTree.getZTreeObj("roleTree"),
                nodes=treeObj.getCheckedNodes(true),
                v="";
            for(var i=0;i<nodes.length;i++){
                v+=','+nodes[i].id;
            }
            if (v.length>0){
                v=v.substr(1);
            }
            $(obj).val(v)
        }
    }).showModal().width(500).height(300);
    $.ajax({
            url: '/api/func/functree',
            dataType: 'json',
            success: function (data) {
                if (mortise_layout.ValidataResult(data)) {
                    if (data.result) {
                        var node =data.result;
                        for(var i=0;i<node.length;i++){
                            node[i].id=node[i].fId;
                            node[i].name=node[i].fName;
                            if (_funcIdList.indexOf(','+node[i].id+',')>-1){
                                node[i].checked=true;
                            }
                            if (node[i].children ){
                                for (var j=0;j<node[i].children.length;j++){
                                    node[i].children[j].id=node[i].children[j].fId;
                                    node[i].children[j].name=node[i].children[j].fName;
                                    if (_funcIdList.indexOf(','+node[i].children[j].id+',')>-1){
                                        node[i].children[j].checked=true;
                                    }
                                }
                            }
                        }
                        zTreeObject= $.fn.zTree.init($("#roleTree"), setting, node);

                        //setCheck();
                    }
                    else {
                        alert('返回数据错误！');
                    }
                }
                else {
                    alert('请求数据失败！');
                }
            }
        }
    );
}
